package com.bw.club.viewModel

import android.util.Log
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.bw.club.model.entity.CarEntity
import com.bw.club.model.service.CarService
import com.bw.club.repository.CarRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class CarViewModel @Inject constructor():ViewModel() {

    @Inject lateinit var carRepository: CarRepository
    //@Inject lateinit var carService: CarService

    private val _fetchSuccessState:MutableLiveData<List<CarEntity>> = MutableLiveData()
    val fetchSuccessState:LiveData<List<CarEntity>>
        get() = _fetchSuccessState

    private val _fetchFailedState:MutableLiveData<Throwable> = MutableLiveData()
    val fetchFailedState:LiveData<Throwable>
        get() = _fetchFailedState



    suspend fun fetchDatas(){

        val result =  carRepository.fetchCarDatas()
        Log.d("re",result.data.toString())
        if (result.code==200){
            _fetchSuccessState.postValue(result.data)//?
        }
        else{
            _fetchFailedState.postValue(RuntimeException("获取列表数据出错，msg:${result.msg}"))
        }
    }


}